package hell.satan.droidbdf.base;

import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.support.annotation.CallSuper;
import android.support.annotation.Nullable;

import hell.satan.droidbdf.utils.Constants;
import hell.satan.droidbdf.utils.LogWrapper;

/**
 * 所有Service基类。
 * Super class for all services.
 *
 * @author Cerberus
 */
@SuppressWarnings("unused")
public class BaseService extends Service {
    protected final String TAG = getClass().getSimpleName();

    @CallSuper
    @Override
    public void onCreate() {
        super.onCreate();
        LogWrapper.v(TAG, "onCreate");
    }

    @CallSuper
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        LogWrapper.v(TAG, "onStartCommand");
        return super.onStartCommand(intent, flags, startId);
    }

    @CallSuper
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        LogWrapper.v(TAG, "onBind");
        return null;
    }

    @CallSuper
    @Override
    public void onDestroy() {
        LogWrapper.v(TAG, "onDestroy");
        super.onDestroy();
    }

    /**
     * 在启动Activity的Intent添加来源信息。
     * Add source information to intent when launch activities.
     *
     * @see #startActivities(Intent[], Bundle)
     */
    @Override
    public void startActivity(Intent intent) {
        startActivity(intent, null);
    }

    /**
     * 在启动Activity的Intent添加来源信息。
     * Add source information to intent when launch activities.
     *
     * @see #startActivities(Intent[], Bundle)
     */
    @Override
    public void startActivity(Intent intent, Bundle options) {
        startActivities(new Intent[]{intent}, options);
    }

    /**
     * 在启动Activity的Intent添加来源信息。
     * Add source information to intent when launch activities.
     *
     * @see #startActivities(Intent[], Bundle)
     */
    @Override
    public void startActivities(Intent[] intents) {
        startActivities(intents, null);
    }

    /**
     * 在启动Activity的Intent添加来源信息。
     * Add source information to intent when launch activities.
     */
    @Override
    public void startActivities(Intent[] intents, Bundle options) {
        String from = getClass().getCanonicalName();
        Intent[] targets = new Intent[intents.length];
        for (int i = 0; i < intents.length; i++) {
            targets[i] = (Intent) intents[i].clone();
            targets[i].addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            targets[i].putExtra(Constants.EXTRA_ACTIVITY_FROM, from);
        }
        super.startActivities(intents, options);
    }

    /**
     * 在启动Service的Intent添加来源信息。
     * Add source information to intent when start service.
     *
     * @see android.app.Activity#startService(Intent)
     */
    @Override
    public ComponentName startService(Intent intent) {
        String from = getClass().getCanonicalName();
        Intent target = (Intent) intent.clone();
        target.putExtra(Constants.EXTRA_ACTIVITY_FROM, from);
        return super.startService(target);
    }

    /**
     * 在绑定Service的Intent添加来源信息。
     * Add source information to intent when bind service.
     *
     * @see android.app.Activity#bindService(Intent, ServiceConnection, int)
     */
    @Override
    public boolean bindService(Intent intent, ServiceConnection conn, int flags) {
        String from = getClass().getCanonicalName();
        Intent target = (Intent) intent.clone();
        target.putExtra(Constants.EXTRA_ACTIVITY_FROM, from);
        return super.bindService(target, conn, flags);
    }
}
